#The dueling program main program.	The magic starts here.
#Authors: Greg Dorman, Lewis Gordon

#re is needed to make binary
from characterclassfiles import warrior_class, wizard_class, rogue_class, paladin_class, witchdoctor_class, soulhunter_class
import misc_functions
import re

if __name__ == "__main__":

	def setClass(name, className):
		if(className.lower() == 'warrior'):
			return warrior_class.WarriorClass(name)
		elif(className.lower() == 'wizard'):
			return wizard_class.WizardClass(name)
		elif(className.lower() == 'rogue'):
			return rogue_class.RogueClass(name)
		elif(className.lower()=='paladin'):
			return paladin_class.PaladinClass(name)
		elif(className.lower()=='witch doctor'):
			return witchdoctor_class.WitchDoctorClass(name)
		elif(className.lower()=='soul hunter'):
			return soulhunter_class.SoulHunterClass(name)	
			
	def printPlayerStats(player):
		print(player.Name + "'s stats (" + player.ClassName.title() + ").")
		print("Stamina: ", player.Stam)
		print("Health: ", player.HP)
		print("Strength: ", player.Str)
		print("Intellect: ", player.Int)
		print("Mana: ", player.Mana)
		print("Spell critical chance: ", player.Scrit, "%")
		print("Agility: ", player.Agi)
		print("Melee critical chance: ", player.Mcrit, "%")
		print("Defense: ", player.Def)
		print("Chance to avoid: ", player.Avd, "%")
		print("Chance to block: ", player.Blk, "%")
		print("Spell Resistance: ", player.Sresist)
		print()
		
	def endTurn(player):
		player.updateBuffs()
		player.updateDebuffs()
		player.updateCooldowns()
		player.updateStunTimer()
	
	#Initializing our player list.
	players = []
	
	#Inputting the number of players to be playing the game.
	print("How many players will be playing?", end=' ')
	playerCount = input()
	
	#Have each player enter their name and class.	There is no
	#spell check on names.	Players have to correctly enter their
	#class name.
	for i in range(int(playerCount)):
		print("Please enter player ", i + 1, "'s name:", end='')
		playerName = input()
		# player = char_classes.Class(playerName)
		while 1:
			className = input("Please select your class [warrior, wizard, rogue, paladin, witch doctor]: ")
			player = setClass(playerName, className)
			if player is not None:
				break
			print("Invalid class name. Try again.")
		players.append(player) 
	
	#Print the stats of each player to the screen.
	for player in players:
		printPlayerStats(player)
	
	playersAlive = len(players)
	# Initiate the main combat sequence.	This will only
	# work when there are more than 1 players alive.
	
	
	
	def printDmgResults(player, Dmg, targetPlayers):
		print(player.Name, " attacks for ", round(Dmg), " damage!")
		for targetPlayer in targetPlayers:
			print(targetPlayer.Name, " has ", round(targetPlayer.HP), " hit points remaining.\n")
	
	
	def GetInputAndExecute(player, players):
		#targetPlayers=[]
		turnInput = misc_functions.getUserInput(player)
		#if(turnInput == "1" or turnInput == "2"):
		#	targetPlayer = misc_functions.choosePlayerToAttack(player, players)
		#	targetPlayers.append(targetPlayer)
		#else: #turnInput == "3"
		#	targetPlayers = misc_functions.choosePlayersToAttack(player,players)
		# Choose player to attack.
		if(turnInput == "1"):
			onCooldown = player.meleeAttack(players)
		if(turnInput == "2"):
			onCooldown = player.useSpell(players)
		if onCooldown:
				GetInputAndExecute(player,players) #repeat code
		
		
	while playersAlive > 1:
		for player in players:
			# If a player is not alive then skip their turn.
			# Dead players will not be removed from the list of
			# players.
			if not player.isAlive():
				continue
				
			if player.isStunned():
				print(player.Name + " is stunned!")
				endTurn(player)
				continue
			# Damage can be greater than or equal than zero. If
			# damage is less than zero their is some error, such as
			# a cooldown.
			#onCooldown = True
			#while onCooldown != False:
				# Get input from each player.
			GetInputAndExecute(player,players)
				
				#if(turnInput == "3"):
				#	Dmg = player.useSpell(targetPlayers)
				#Calculates the targeted player's remaining hp based on calculated damage
				#and their defense.
				#for tPlayer in targetPlayers:
					#tPlayer.HP -= (Dmg - tPlayer.Def)
					# If the player's health drops below zero keep it capped at zero.
				#	if(tPlayer.HP < 0):
					#	tPlayer.HP = 0
					
			
			# Print results of attack or spell.
			#printDmgResults(player, Dmg, targetPlayers)
			
			# Since dead players are not removed from the list of players we use an
			# integer counter instead.
			for player_ in players:
				if not player_.isAlive():
					playersAlive-=1
			endTurn(player)
	
	# Slightly inefficient but a convenient way to check the last remaining alive player.
	for player in players:
		if not player.isAlive():
			continue
		print(player.Name + " is victorious!")
		break
	
	# Used for executables and stand alone consoles.
	exitInput = None
	while(exitInput is None):
		exitInput = input('Press any key to exit.')
